package com.example.widget_lib.widget.progress.horbar;

import com.example.widget_lib.bean.GradientColor;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ProgressBar;
import ohos.agp.render.Canvas;
import ohos.agp.render.LinearShader;
import ohos.agp.render.Paint;
import ohos.agp.render.Shader;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

public class SectionProBar extends ProgressBar implements Component.DrawTask {

    private Rect bounds;
    private Paint mBgPaint;
    private Paint mProPaint;
    private GradientColor mBgGradientColor; // 背景颜色渐变对象
    private GradientColor mProGradientColor; // 进度颜色渐变对象
    private float progress;

    public SectionProBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initPaint();
        addDrawTask(this);
    }

    public SectionProBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initPaint();
        addDrawTask(this);
    }

    /**
     * 设置进度数值
     *
     * @param progress
     */
    public void setProgress(float progress) {
        this.progress = progress;
        setProgressValue((int) progress);
        invalidate();
    }

    /**
     * 初始化画笔
     */
    public void initPaint() {
        mBgPaint = new Paint();
        mBgPaint.setStrokeJoin(Paint.Join.ROUND_JOIN);
        mBgPaint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        mBgPaint.setStyle(Paint.Style.FILL_STYLE);
        mBgPaint.setAntiAlias(true);

        mProPaint = new Paint();
        mProPaint.setStrokeJoin(Paint.Join.ROUND_JOIN);
        mProPaint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        mProPaint.setStyle(Paint.Style.FILL_STYLE);
        mProPaint.setAntiAlias(true);
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        task.onDraw(this, mCanvasForTaskOverContent);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        bounds = getProgressElement().getBounds();
        float right = progress * bounds.right / getMax();
        if (right > bounds.right) {
            right = bounds.right;
        }
        //绘制背景条
        drawProgressBg(canvas, right);
        //绘制进度条
        drawProgress(canvas, right);
    }

    /**
     * 绘制进度条背景
     *
     * @param canvas
     * @param reachedEndX
     */
    private void drawProgressBg(Canvas canvas, float reachedEndX) {
        if (mBgGradientColor == null) { // 默认
            mBgPaint.setColor(new Color(Color.getIntColor("#58b5fa")));
            drawNormalShader(canvas, bounds.right, mBgPaint);
        } else { // 渐变
            float temp = bounds.right - reachedEndX;
            drawLinearShader(canvas, temp, bounds.right, mBgGradientColor, mBgPaint);
        }
    }

    /**
     * 绘制当前进度条
     *
     * @param canvas
     * @param reachedEndX
     */
    private void drawProgress(Canvas canvas, float reachedEndX) {
        if (reachedEndX > 0) {// 默认
            if (mProGradientColor == null) {
                mProPaint.setColor(new Color(Color.getIntColor("#FF6853")));
                drawNormalShader(canvas, reachedEndX, mProPaint);
            } else {// 渐变
                drawLinearShader(canvas, 0, reachedEndX, mProGradientColor, mProPaint);
            }
        }
    }

    /**
     * 绘制纯色的矩形
     *
     * @param canvas
     * @param right
     * @param paint
     */
    private void drawNormalShader(Canvas canvas, float right, Paint paint) {
        Rect bounds = getProgressElement().getBounds();
        RectFloat rectf = new RectFloat(bounds.left, bounds.top, right, bounds.bottom);
        float radius = (bounds.bottom - bounds.top) >> 1;
        canvas.drawRoundRect(rectf, radius, radius, paint);
    }

    /**
     * 绘制线性渐变的矩形
     *
     * @param canvas
     * @param left
     * @param right
     * @param color
     * @param paint
     */
    private void drawLinearShader(Canvas canvas, float left, float right, GradientColor color, Paint paint) {
        RectFloat rectf = new RectFloat(bounds.left, bounds.top, right, bounds.bottom);
        float radius = (bounds.bottom - bounds.top) >> 1;
        //添加线性渐变的Shader
        Point sp = new Point(left, bounds.bottom);
        Point ep = new Point(right, bounds.bottom);
        Point[] points = new Point[]{sp, ep};
        Color sc = new Color(color.getStartColor());
        Color ec = new Color(color.getEndColor());
        Color[] colors = new Color[]{sc, ec};
        LinearShader shader = new LinearShader(points, null, colors, Shader.TileMode.CLAMP_TILEMODE);
        paint.setShader(shader, Paint.ShaderType.LINEAR_SHADER);
        //绘制圆角矩形
        canvas.drawRoundRect(rectf, radius, radius, paint);
    }

    /**
     * 设置渐变颜色
     *
     * @param startColor
     * @param endColor
     */
    public void setGradientBgColor(int startColor, int endColor) {
        mBgGradientColor = new GradientColor(startColor, endColor);
    }

    /**
     * 设置渐变颜色
     *
     * @param startColor
     * @param endColor
     */
    public void setGradientProColor(int startColor, int endColor) {
        mProGradientColor = new GradientColor(startColor, endColor);
    }

    /**
     * 设置进度条动画
     *
     * @param progress
     */
    public void setProgressAnim(float progress) {
        AnimatorValue animator = new AnimatorValue();

        animator.setValueUpdateListener((animatorValue, v) -> setProgress(progress * v));
        animator.setDuration(1200);
        animator.start();
    }
}
